package cn.com.codingce.student.servlet;

import cn.com.codingce.student.entity.Student;
import cn.com.codingce.student.service.IStudentService;
import cn.com.codingce.student.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /*
     * 修改
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        /*
         * 获取修改人的学号
         */
        int no = Integer.parseInt(request.getParameter("sno"));
        /*
         * 修改后的内容
         */
        int sno = Integer.parseInt(request.getParameter("sno"));
        String name = request.getParameter("sname");
        int age = Integer.parseInt(request.getParameter("sage"));
        String address = request.getParameter("saddress");
        /*
         * 将修改的内容封装到javaBean中
         */
        Student student = new Student(sno, name, age, address);

        IStudentService service = new StudentServiceImpl();
        boolean result = service.updateStudentBySno(no, student);

        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        if (result) {

            /*
             * response.getWriter().println("修改成功!");
             * 修改完成后，再次全部查询
             */
            response.sendRedirect("querybypage");
        } else {
            response.getWriter().println("修改失败!");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
